package com.hwhhhh.mygrad.adapter;

import android.content.Intent;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hwhhhh.mygrad.ClsInfoActivity;
import com.hwhhhh.mygrad.HistoryActivity;
import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.dto.AppointDTO;
import com.hwhhhh.mygrad.utils.StringUtils;
import com.hwhhhh.mygrad.view.IconfontView;

import java.sql.Date;
import java.util.List;

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryHolder> {
    private View itemView;
    private List<AppointDTO> data;

    public HistoryAdapter(List<AppointDTO> data) {
        this.data = data;
    }

    @NonNull
    @Override
    public HistoryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history, parent, false);
        return new HistoryHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull HistoryHolder holder, int position) {
        AppointDTO appointDTO = data.get(position);
        switch (appointDTO.getState()) {
            case -1:
                holder.state.setText("状态：已取消");
                break;
            case 0:
                holder.state.setText("状态：尚未占用");
                break;
            case 1:
                holder.state.setText("状态：正在占用");
                break;
            case 2: // 有预订，已结束占用
                String str = "占用时间：" + StringUtils.dateFormat(appointDTO.getOccupyStartTime(), "HH:mm") + "~" + StringUtils.dateFormat(appointDTO.getOccupyEndTime(), "HH:mm");
                holder.state.setText(str);
                break;
        }
        String typeStr = "空闲课时：" + StringUtils.getStrCls(appointDTO.getFreeClassroomEntity().getFreeCls());
        holder.type.setText(typeStr);
        holder.clsNum.setText(appointDTO.getFreeClassroomEntity().getClassroomNum());
        holder.rootView.setOnClickListener(v -> {
            HistoryActivity historyActivity = (HistoryActivity) itemView.getContext();
            Intent intent = new Intent(historyActivity, ClsInfoActivity.class);
            intent.putExtra("freeClsId", appointDTO.getFreeClassroomId());
            intent.putExtra("clsId", appointDTO.getFreeClassroomEntity().getClassroomId());
            intent.putExtra("date", appointDTO.getFreeDate());
            historyActivity.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class HistoryHolder extends RecyclerView.ViewHolder {
        TextView clsNum, type, state;
        View rootView;

        public HistoryHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView;
            clsNum = itemView.findViewById(R.id.tv_history_cls_num);
            type = itemView.findViewById(R.id.tv_history_type);
            state = itemView.findViewById(R.id.tv_history_state);
        }
    }

    class HistoryHeader extends RecyclerView.ViewHolder {
       TextView date, year;

        public HistoryHeader(@NonNull View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.history_title_date);
            year = itemView.findViewById(R.id.history_title_year);
        }
    }

    private String getDateStr(Date date, int start, int end) {
        String str = date.toString();
        return str + " " + getStartTime(start) + " 至 " + getEndTime(end);
    }

    private String getStartTime(int type) {
        switch (type) {
            case 1:
                return "8:00";
            case 2:
                return "8:50";
            case 3:
                return "9:50";
            case 4:
                return "10:40";
            case 5:
                return "11:30";
            case 6:
                return "13:30";
            case 7:
                return "14:20";
            case 8:
                return "15:20";
            case 9:
                return "16:10";
            case 10:
                return "18:30";
            case 11:
                return "19:20";
            case 12:
                return "20:10";
            default:
                return "21:00";
        }
    }

    private String getEndTime(int type) {
        switch (type) {
            case 1:
                return "8:45";
            case 2:
                return "9:35";
            case 3:
                return "10:35";
            case 4:
                return "11:25";
            case 5:
                return "12:15";
            case 6:
                return "14:15";
            case 7:
                return "15:05";
            case 8:
                return "16:05";
            case 9:
                return "16:55";
            case 10:
                return "19:15";
            case 11:
                return "20:05";
            case 12:
                return "20:55";
            default:
                return "21:45";
        }
    }
}
